/*
 Xern Entity Relocation Network
 Copyright (C) 2010-2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#include <stdio.h>
#include "Xern/FileSystem.hpp"

#if defined(linux)

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

namespace Xern
{
	void ReadDirectory(const std::string &path, DirectoryElements &out)
	{
		// Open the directory.
		DIR *dir = opendir(path.c_str());
		if(!dir)
			return;

		// Iterate through each directory element.
		dirent *ent = readdir(dir);
		for(; ent ; ent = readdir(dir))
			out.push_back(ent->d_name);

		// Close the directory.
		closedir(dir);
	}

	std::string DirName(const std::string &path)
	{
		return path.substr(0, path.rfind('/'));
	}

	std::string BaseName(const std::string &path)
	{
		size_t pos = path.rfind('/');
		if(pos != std::string::npos)
			pos++;
		return path.substr(pos);
	}

	std::string FileExtension(const std::string &path)
	{
		size_t pos = path.rfind('.');
		if(pos != std::string::npos)
			return path.substr(pos);
		else
			return "";
	}

	std::string FileWithoutExtension(const std::string &path)
	{
		return path.substr(0, path.rfind('.'));
	}

	std::string JoinPath(const std::string &p1, const std::string &p2)
	{
		// Check for empty paths.
		if(p1.empty())
			return p2;

		if(p2.empty())
			return p1;

		// Check if the last character in p1 is a separator.
		if(p1[p1.size()-1] == '/')
		{
			// Check if the first character in p2 is a separator.
			if(p2[0] == '/')
			{
				// Append them, but remove the first character in p2.
				return p1 + p2.substr(1);
			}

			// Just append them.
			return p1 + p2;
		}
		else
		{
			// Check if the first character in p2 is a separator.
			if(p2[0] == '/')
			{
				// Just append them.
				return p1 + p2;
			}
			// Add the path separator.
			return p1 + "/" + p2;
		}
	}

	bool IsAbsolutePath(const std::string &path)
	{
		if(path.empty())
			return false;
		return path[0] == '/';
	}

	bool PathExists(const std::string &path)
	{
		// Perform a stat.
		struct stat data;
		int ret = stat(path.c_str(), &data);

		// Check the return code, if success it means that the file exists,
		// otherwise it doesn't exist.
		if(ret != 0)
			return false;
		return true;
	}

	void MakeDirectory(const std::string &path)
	{
		mkdir(path.c_str(), 0766);
	}

	bool IsDirectory(const std::string &path)
	{
		// Perform a stat.
		struct stat data;
		int ret = stat(path.c_str(), &data);
		if(ret != 0)
			return false;

		// Check if the path is a directory.
		return S_ISDIR(data.st_mode);
	}

	bool IsFile(const std::string &path)
	{
		// Perform a stat.
		struct stat data;
		int ret = stat(path.c_str(), &data);
		if(ret != 0)
			return false;

		// Check if the path is a regular file.
		return S_ISREG(data.st_mode);
	}

} // namespace Xern

#endif
